package com.canbot.u05.activity.shoppingGuide.codeMarketing;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.guide.GuideChoosePicActivity;
import com.canbot.u05.bean.codeMarketing.CodeMarketingInfo;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.utils.ToastTools;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zmp
 * 二维码营销设置界面
 */
public class CodeMarketingActivity extends BaseCodeFinishActivity {

        @BindView(R.id.code_iv_back)
        Button codeIvBack;

        @BindView(R.id.code_iv_next)
        Button codeIvNext;

        @BindView(R.id.code_gv)
        GridView codeGv;

        private int currentPosition = -1;

        private ToastTools toastTools;

        private static final int Q_CODE_BG_FREE = 1001;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_code_market);
                ButterKnife.bind(this);
                toastTools = ToastTools.newInstance(this);
                initView();
                MapVoiceAndEyes.CODE_MARKETING[(int) (Math.random() * 2)].playMapVoiceAndEyes();
        }

        private void initView() {
                CodeMarketingInfo marketingSave = CodeDataHelper.getInstance().getCodeMarketingSave();
                if (marketingSave != null) {
                        currentPosition = marketingSave.getType();
                }
                codeGv.setAdapter(baseAdapter);
                codeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                currentPosition = position;
                                baseAdapter.notifyDataSetChanged();
                        }
                });
        }

        private BaseAdapter baseAdapter = new BaseAdapter() {
                @Override
                public int getCount() {
                        return 4;
                }

                @Override
                public Object getItem(int position) {
                        return position;
                }

                @Override
                public long getItemId(int position) {
                        return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        if (convertView == null) {
                                convertView = View.inflate(parent.getContext(), R.layout.code_gv_item, null);
                                convertView.setTag(new Holder(convertView));
                        }
                        Holder holder = (Holder) convertView.getTag();
                        holder.upDate(position);
                        return convertView;
                }
        };

        private class Holder {

                private ImageView bgIv;

                private ImageView choiceIv;

                Holder(View convertView) {
                        bgIv = (ImageView) convertView.findViewById(R.id.code_iv_bg);
                        choiceIv = (ImageView) convertView.findViewById(R.id.code_iv_choice);
                }

                public void upDate(int position) {
                        choiceIv.setVisibility(currentPosition == position ? View.VISIBLE : View.GONE);
                        switch (position) {
                                case 0:
                                        bgIv.setImageResource(R.drawable.code_item_bg01);
                                        break;
                                case 1:
                                        bgIv.setImageResource(R.drawable.code_item_bg02);
                                        break;
                                case 2:
                                        bgIv.setImageResource(R.drawable.code_item_bg03);
                                        break;
                                case 3:
                                        bgIv.setImageResource(R.drawable.code_item_bg04);
                                        break;
                                default:
                                        bgIv.setImageResource(R.drawable.code_item_bg04);
                                        break;
                        }
                }
        }

        @OnClick({R.id.code_iv_back, R.id.code_iv_next})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.code_iv_back:
                                finish();
                                break;
                        case R.id.code_iv_next:
                                if (currentPosition == -1) {
                                        toastTools.showText(getString(R.string.select_corresponding_template));
                                        return;
                                }
                                switch (currentPosition) {
                                        case 0:
                                                CodeDataHelper.getInstance().getCodeMarketingInfo().setType(0);
                                                Intent intent = new Intent(this, GuideChoosePicActivity.class);
                                                intent.putExtra("multiple", true);
                                                CodeMarketingInfo codeMarketingSave = CodeDataHelper.getInstance().getCodeMarketingSave();
                                                if (codeMarketingSave != null) {
                                                        List<String> imagePaths = codeMarketingSave.getImagePaths();
                                                        if (null != imagePaths && imagePaths.size() > 0) {
                                                                intent.putExtra("picArray", JSON.toJSONString(imagePaths));
                                                        }
                                                }
                                                MapVoiceAndEyes.CODE_MARKETING[(int) (Math.random() * 2) + 2].playMapVoiceAndEyes();
                                                startForResultActivity(intent, Q_CODE_BG_FREE);
                                                break;
                                        case 1:
                                                CodeDataHelper.getInstance().getCodeMarketingInfo().setType(1);
                                                startOtherActivity(CodeModuleActivity.class, false);
                                                break;
                                        case 2:
                                                CodeDataHelper.getInstance().getCodeMarketingInfo().setType(2);
                                                startOtherActivity(CodeModuleActivity.class, false);
                                                break;
                                        case 3:
                                                CodeDataHelper.getInstance().getCodeMarketingInfo().setType(3);
                                                startOtherActivity(CodeModuleActivity.class, false);
                                                break;
                                }

                                break;
                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                CodeMarketingInfo codeMarketingInfo = CodeDataHelper.getInstance().getCodeMarketingInfo();
                if (requestCode == Q_CODE_BG_FREE && resultCode == RESULT_OK) {
                        String picPath = data.getStringExtra("picArray");
                        codeMarketingInfo.setImagePaths(JSON.parseArray(picPath, String.class));
                        MapVoiceAndEyes.CODE_MARKETING[(int) (Math.random() * 2) + 4].playMapVoiceAndEyes();
                        Intent intent = new Intent(this, CodeVoiceSetActivity.class);
                        CodeMarketingInfo codeMarketingSave = CodeDataHelper.getInstance().getCodeMarketingSave();
                        if (codeMarketingSave != null) {
                                intent.putExtra("path", codeMarketingSave.getVoiceContent());
                                intent.putExtra("content", codeMarketingSave.getVoiceContent());
                                intent.putExtra("type", codeMarketingSave.getVoiceType());
                        }
                        startOtherActivity(intent, false);
                }
        }
}
